<template>
  <div class="app-container">
<!--    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">-->
<!--      <el-form-item label="塔ID" prop="towerId">-->
<!--        <el-input-->
<!--          v-model="queryParams.towerId"-->
<!--          placeholder="请输入塔ID"-->
<!--          clearable-->
<!--          -->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="赏品名称" prop="goodsName">-->
<!--        <el-input-->
<!--          v-model="queryParams.goodsName"-->
<!--          placeholder="请输入赏品名称"-->
<!--          clearable-->
<!--          -->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="赏品数量" prop="goodsNum">-->
<!--        <el-input-->
<!--          v-model="queryParams.goodsNum"-->
<!--          placeholder="请输入赏品数量"-->
<!--          clearable-->
<!--          -->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="赏品价格" prop="goodsPrice">-->
<!--        <el-input-->
<!--          v-model="queryParams.goodsPrice"-->
<!--          placeholder="请输入赏品价格"-->
<!--          clearable-->
<!--          -->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="回收价格" prop="outPrice">-->
<!--        <el-input-->
<!--          v-model="queryParams.outPrice"-->
<!--          placeholder="请输入回收价格"-->
<!--          clearable-->
<!--          -->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="上架ID" prop="shangId">-->
<!--        <el-input-->
<!--          v-model="queryParams.shangId"-->
<!--          placeholder="请输入上架ID"-->
<!--          clearable-->
<!--          -->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="排序" prop="sort">-->
<!--        <el-input-->
<!--          v-model="queryParams.sort"-->
<!--          placeholder="请输入排序"-->
<!--          clearable-->
<!--          -->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="真实概率" prop="realPro">-->
<!--        <el-input-->
<!--          v-model="queryParams.realPro"-->
<!--          placeholder="请输入真实概率"-->
<!--          clearable-->
<!--          -->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="显示概率" prop="lookPro">-->
<!--        <el-input-->
<!--          v-model="queryParams.lookPro"-->
<!--          placeholder="请输入显示概率"-->
<!--          clearable-->
<!--          -->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="出奖门槛" prop="menkan">-->
<!--        <el-input-->
<!--          v-model="queryParams.menkan"-->
<!--          placeholder="请输入出奖门槛"-->
<!--          clearable-->
<!--          -->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="塔层" prop="level">-->
<!--        <el-input-->
<!--          v-model="queryParams.level"-->
<!--          placeholder="请输入塔层"-->
<!--          clearable-->
<!--          -->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item>-->
<!--        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
<!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
<!--      </el-form-item>-->
<!--    </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:tagoods:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:tagoods:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:tagoods:remove']"
        >删除</el-button>
      </el-col>
      <el-tag>当前位置显示概率为：{{seLookTotalPro}}%</el-tag> ||
      <el-tag type="success" >当前位置真实概率为：{{setTotalPro}}%</el-tag>

<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:tagoods:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tagoodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="塔ID" align="center" prop="towerId" />
      <el-table-column label="赏图片" align="center" prop="goodsImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.goodsImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="赏品名称" align="center" prop="goodsName" />
      <el-table-column label="展示价格" align="center" prop="goodsPrice" />
      <el-table-column label="回收价格" align="center" prop="outPrice" >
        <template slot-scope="scope">
          <el-tag type="success"   effect="dark"> {{scope.row.outPrice}}RMB </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="赏类型" align="center" prop="shangId" >
          <template slot-scope="scope">
            {{ class_shang[scope.row.shangId] }}
          </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <el-tag type="success"  v-if="scope.row.status === 1">上架</el-tag>
          <el-tag type="danger"  v-if="scope.row.status === 0">下架</el-tag>
        </template>
      </el-table-column>


      <el-table-column label="显示概率" align="center" prop="lookPro" />
      <el-table-column label="真实概率" align="center" prop="realPro" >
        <template slot-scope="scope">
          <el-tag type="danger"  effect="light"> {{scope.row.realPro*100}}% </el-tag>
        </template>

      </el-table-column>
      <el-table-column label="出奖门槛" align="center" prop="menkan" width="220">
        <template slot-scope="scope">
         门槛金额大于等于 <el-tag type="danger"  effect="dark"> {{scope.row.menkan}}RMB </el-tag> 时出奖
        </template>


      </el-table-column>
      <el-table-column label="归属塔层" align="center" prop="level" width="150">
        <template slot-scope="scope">
          <el-tag type="warning"  effect="dark"> 当前层级：【{{scope.row.level}} 】层</el-tag>

        </template>


      </el-table-column>
      <el-table-column label="赏品属性" align="center" prop="remark" >
        <template slot-scope="scope">
          <el-tag  type="info"  v-if="scope.row.remark === '0'">平</el-tag>
          <el-tag  type="success"  v-if="scope.row.remark > 0">上升{{scope.row.remark}}层</el-tag >
          <el-tag  type="danger" v-if="scope.row.remark < 0">下降{{-scope.row.remark}}层</el-tag >
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:tagoods:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:tagoods:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改塔赏品实体对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="赏品名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入赏品名称" />
        </el-form-item>
        <el-form-item label="赏品图片">
          <image-upload v-model="form.goodsImg"/>
        </el-form-item>

        <el-form-item label="展示价格" prop="goodsPrice">
          <el-input-number :step="5" :precision="2" v-model="form.goodsPrice" placeholder="请输入赏品价格" />
        </el-form-item>
        <el-form-item label="回收价格" prop="outPrice">
          <el-input-number :step="5" :precision="2" v-model="form.outPrice" placeholder="请输入回收价格" />
        </el-form-item>
        <el-form-item label="关联赏类型" prop="shangId">
          <el-select v-model="form.shangId" clearable placeholder="请选择赏类型" >
            <el-option v-for="item in shang_option" :key="item.label" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="显示概率" prop="lookPro">
          <el-input-number :step="0.1" :precision="5" :min="0" :max="1" v-model="form.lookPro" placeholder="请输入显示概率" />
        </el-form-item>

        <el-form-item label="真实概率" prop="realPro">
          <el-input-number :step="0.1" :precision="5"  :min="0" :max="1" v-model="form.realPro" placeholder="请输入真实概率" />
        </el-form-item>

        <el-form-item label="出奖门槛" prop="menkan">
          <el-input-number :step="100" :precision="0" v-model="form.menkan" placeholder="请输入出奖门槛" />
        </el-form-item>

        <el-form-item label="排序" prop="sort">
          <el-input-number :step="1" v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>


        <el-form-item label="赏品属性" prop="remark">
            <el-input-number :step="1" v-model="form.remark"  placeholder="请输入排序" />
        </el-form-item>

        <el-form-item label="赏品状态" prop="status">
          <el-switch v-model="form.status===1"  active-text="上架" inactive-text="下架"  @change="handleStatusChange"      active-color="#13ce66" inactive-color="#ff4949" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTagoods, getTagoods, delTagoods, addTagoods, updateTagoods } from "@/api/system/tagoods";
import {getType} from "@/api/wxs/add_good";

export default {
  name: "Tagoods",
  data() {
    return {
      //赏类型对象
      class_shang: {},
      shang_option: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 塔赏品实体表格数据
      tagoodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        towerId: null,
        goodsImg: null,
        goodsName: null,
        goodsNum: null,
        goodsPrice: null,
        outPrice: null,
        shangId: null,
        status: null,
        sort: null,
        realPro: null,
        lookPro: null,
        menkan: null,
        level: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        remark: [
          { required: true, message: "0平 1上升 2下降不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getType();
    this.form.level = this.$route.params.levelId
  },
  computed: {
    setTotalPro() {
      let totalPro = 0
      this.tagoodsList.forEach(item => {
        totalPro += item.realPro
      })
      totalPro = totalPro*100
      return totalPro.toFixed(5)
    },
    seLookTotalPro() {
      let totalPro = 0
      this.tagoodsList.forEach(item => {
        totalPro += item.lookPro
      })
      totalPro = totalPro*100
    return totalPro.toFixed(5)
    }
  },
  methods: {
    handleStatusChange(val) {
      this.form.status = val? 1 : 0
    },
    //获取赏类型
    getType() {
      getType().then((res) => {
        console.log("rows is",res.rows)
        this.typeList = res.rows
        this.typeList.filter(({ id }) => ![1, 2, 3, 4, 31,32].includes(id)).forEach((item) => {
          this.class_shang[item.id] = item.title
        })
        this.shang_option = this.typeList
          .filter(({ id }) => ![1, 2, 3, 4, 31,32].includes(id))
          .map(({ id, title }) => ({ value: id, label: title }));

        console.log("this.class_shang is",this.class_shang)
      })
    },
    /** 查询塔赏品实体列表 */
    getList() {
      console.log("this.$route.params is",this.$route.params)
      this.loading = true;
      this.queryParams.level = this.$route.params.levelId
      this.queryParams.towerId = this.$route.params.towerId
      this.queryParams.orderByColumn = "sort"
      this.queryParams.isAsc = "asc"

      listTagoods(this.queryParams).then(response => {
        this.tagoodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        towerId: this.$route.params.towerId,
        goodsImg: null,
        goodsName: null,
        goodsNum: null,
        goodsPrice: null,
        outPrice: null,
        shangId: null,
        status: 0,
        sort: null,
        realPro: null,
        lookPro: null,
        menkan: null,
        level: this.$route.params.levelId,
        createTime: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加塔赏品实体";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getType();
      const id = row.id || this.ids

      getTagoods(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改塔赏品实体";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTagoods(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTagoods(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除塔赏品实体编号为"' + ids + '"的数据项？').then(function() {
        return delTagoods(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/tagoods/export', {
        ...this.queryParams
      }, `tagoods_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
